iT邦幫忙

2022 iThome 鐵人賽

DAY 27
0
自我挑戰組

web 應用開發筆記系列 第 27

[Day 27] Building Landing Page Environment

  • 分享至 

  • xImage
  •  

The dir tree is :

  • app.js
  • config (dir)
  • package.json
  • src
    • app.jsx
    • components
    • images
    • less
  • views
    • index.pug
    • layout.pug
  • webpack.config.js

(1) npm intit

Init project with npm init

npm init [-y]

(2) install and start koa

install koa with ==install==

npm install koa koa-convert koa-router koa-static koa-views -S

Koa

koa-convert

koa-router

koa-static

koa-views

(3) Use Config

using the npm package named config (website)

npm install config -S

安裝完建立 default.js,此處與底下的官方教學不同,這邊使用 js 模組的方式,不過基本上與下方 JSON 方式相同,模組釋出的也是一個 JS Object

module.exports = {
    'service': {
        'name': 'React',
        'external_url': 'http://localhost:3001'
    },
    'server': {
        'port': 3001
    }
}

Open the app.js in root dir and coding using the code above

Config

此工具可以幫助你把所有的相關設定都整理到同一個檔案中,下是官方的教學內容

  1. install it

    $ npm install config
    
  2. create a folder to locate the config file

    $ mkdir config
    
  3. Edit the file named ==default,json==

    $ vim config/default.json
    
  4. The file look like:

    {
      // Customer module configs
      "Customer": {
        "dbConfig": {
          "host": "localhost",
          "port": 5984,
          "dbName": "customers"
        },
        "credit": {
          "initialLimit": 100,
          // Set low for development
          "initialDays": 1
        }
      }
    }
    
  5. Use configs in your code

    var config = require('config');
    //...
    var dbConfig = config.get('Customer.dbConfig');
    db.connect(dbConfig, ...);
    

(4) Configure the Routes

直接使用第二步驟安裝的 ==koa-router== 來設定首頁的路徑,直接使用 GET 來渲染

const Koa = require('koa');
const convert = require('koa-convert');
const serve = require('koa-static');
const path = require('path');
const config = require('config');

const app = new Koa();

app.use(convert(serve(path.join(__dirname, 'public'))));

app.use( ctx => {
    ctx.body = 'Hello World'
})

app.listen(config.server.port, _=>{
    console.log(`server starting on ${config.server.port}`)
})

接著使用 kuo 中的 Router 來控制 koa 應用的路由 (與 express router 類似,可用 async)

const Koa = require('koa');
const convert = require('koa-convert');
const serve = require('koa-static');
const Router = require('koa-router');
const path = require('path');
const config = require('config');

const app = new Koa();
const router = new Router();

app.use(convert(serve(path.join(__dirname, 'public'))));

router.get('/', async (ctx, next) => {
    ctx.body = 'Hello world';
});

app.use(router.routes());

app.listen(config.server.port, _=>{
    console.log(`server starting on ${config.server.port}`)
})

(5) Install and Use Template Engine - Pug

安裝 Pug 套件,此套件是模板引擎 Jade 的前身,可用 ejs 代替(與 koa 的配合可以再實驗)

$ npm install pug -S

在 views 新增 layout.pug 檔案並利用 CDN 引入 JQiery, Semantic UI

doctype html
html
    head
        meta(http-equiv='content-type', content='text/html')
        meta(name='viewport', content='width=device-width, initial-scale=1.0')
        meta(http-equiv='X-UA-Compatible', content='IE=edge')

        title LandingPage

        link(href='https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.9/semantic.min.css', mefia='all', rel='stylesheet', type='text/css')
        script(type='text/javascript', src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js')
        script(type='text/javascript', src='https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.9/semantic.min.js')

        block head

    body
        block body

在 views 中在建立一個 index.pug

extends layout

block body
    h1 Hello Pug!!

在 app.js 中使用 koa-views 並且將 pug 的內容渲染到前端

const views = require('koa-views');

//Render Pug
app.use(views(path.join(__dirname, 'views'), {extension: 'pug'}));

router.get('/', async (ctx, next) => {
    await ctx.render('index');
});

直得注意的是這邊的渲染利用到 await 的方法等待上一個 middleware 做完後做渲染,達到同步執行的效果。

(6) Use Webpack2 to build the env including React and ES7

使用 webpack2 建置好 React 的開發環境,安裝建置環境相關的工具

  • babel-polyfill
  • babel-cli
  • babel-core
  • babel-loader
  • babel-preset-es2017
  • babel-preset-react
  • babel-preset-stage-0
  • react
  • react-dom
  • webpack
$ npm install babel-polyfill babel-cli babel-core babel-loader babel-preset-es2017 babel-preset-react babel-preset-stage-0 react react-dom webpack -S

在根目錄新增 web pack.config.js 並且設定其內容

var path = require('path')
var webpack = require('webpack')

module.exports - {
    context: path.join(__dirname, 'src'),
    entry: {
        bundle: './app.jsx',
        vendors: [
            'bable-polyfill',
            'react',
            'react-dom'
        ]
    },
    output: {
        filename: '[name].js',
        publishPath: '/assets/',
        path: path.join(__dirname, 'public', 'assets')
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                enforce: 'pre',
                loader: 'babel-loader',
                options: {
                    pressets : [ 'react', 'es2017', 'stage-0' ]
                }
            }
        ],
        plugin: [
            new webpack.optimize.CommonsChunkPlugin({
                name: 'vendors'     //Specify the common bundle's name
            })
        ],
        externals: {
            config: JSON.stringify(require('config'))
        },
        resolve: {
            extensions: ['.js', '.jsx'],
            alias: {
                'react$': 'react/dist/react.min.js',
                'react-dom$': 'react-dom/dist/react-dom.min.js'
            }
        }
    }
}

使用局部的 webpack 打包

$ ./node_modules/.bin/webpack

以上會有錯誤,解決中,目前應該要確定 webpack 的版本是 2.3.2

錯誤如下:

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration.module has an unknown property 'resolve'. These properties are valid:
   object { exprContextCritical?, exprContextRecursive?, exprContextRegExp?, exprContextRequest?, loaders?, noParse?, rules?, unknownContextCritical?, unknownContextRecursive?, unknownContextRegExp?, unknownContextRequest?, unsafeCache?, wrappedContextCritical?, wrappedContextRecursive?, wrappedContextRegExp?, strictExportPresence? }
   Options affecting the normal modules (`NormalModuleFactory`).
 - configuration.output has an unknown property 'publishPath'. These properties are valid:
   object { auxiliaryComment?, chunkFilename?, crossOriginLoading?, devtoolFallbackModuleFilenameTemplate?, devtoolLineToLine?,devtoolModuleFilenameTemplate?, filename?, hashDigest?, hashDigestLength?, hashFunction?, hotUpdateChunkFilename?, hotUpdateFunction?, hotUpdateMainFilename?, jsonpFunction?, library?, libraryTarget?, path?, pathinfo?, publicPath?, sourceMapFilename?, sourcePrefix?, strictModuleExceptionHandling?, umdNamedDefine? }
   Options affecting the output of the compilation. `output` options tell webpack how to write the compiled files to disk.

上一篇
[Day 26] Note - NPM Start
下一篇
[Day 28] Unit test
系列文
web 應用開發筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言